import {IconifyJSON} from '@iconify/types'
import {NumUtil} from '@xtail/core'
import {IconIconifyPropsType, IconTypeConst} from '../../../components'
import {IconsType} from '../../../common'

/**
 * 根据 IconifyJSON 生成图标列表（列表中的元素为 SIcon 组件的组件参数外加一个 iconName 属性）
 *
 * @param {IconifyJSON} iconifyJSON IconifyJSON 对象
 * @param {Omit<IconIconifyPropsType, "icon">} iconIconifyProps 图标组件 SIconIconify
 * 的组件参数，其中 icon 组件参数已被忽略，该参数值会从 IconifyJSON 中获取
 * @returns {IconPropsType[]} 图标列表
 */
export const genIconsByIconifyJSON = (
  iconifyJSON: IconifyJSON,
  iconIconifyProps: Omit<IconIconifyPropsType, 'icon'> = {}
): IconsType => {
  const icons: IconsType = []
  for (const iconName of Object.keys(iconifyJSON.icons)) {
    icons.push({
      iconName: iconifyJSON.prefix + ':' + iconName,
      iconType: IconTypeConst.ICONIFY,
      iconProps: {
        ...iconIconifyProps,
        icon: {
          body: iconifyJSON.icons[iconName].body,
          width:
            iconifyJSON.icons[iconName]?.width ??
            iconifyJSON?.width ??
            (NumUtil.isNum(iconIconifyProps?.width) ? iconIconifyProps?.width : undefined) ??
            24,
          height:
            iconifyJSON.icons[iconName]?.height ??
            iconifyJSON?.height ??
            (NumUtil.isNum(iconIconifyProps?.height) ? iconIconifyProps?.height : undefined) ??
            24
        }
      }
    })
  }
  return icons
}

export type GenIconsByIconifyJSONType = typeof genIconsByIconifyJSON
